package com.jjc.android.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.allen.library.SuperButton
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.activity.DetectionActivity
import com.jjc.android.base.BaseActivity
import com.jjc.android.base.BaseLazyLoadFragment
import com.jjc.android.dialog.MessageImageDialog
import com.jjc.android.entity.KeyValuePair
import com.jjc.android.entity.LzyResponse
import com.jjc.android.entity.PageInfo
import com.jjc.android.entity.SamplingPageDto
import com.jjc.android.net.SimpleHttpCallback
import com.jjc.android.util.AndroidBug5497Workaround
import com.jjc.android.util.Common.parseEmpty
import com.jjc.android.util.MultiLanguageUtil
import com.jjc.android.util.messenger.ActivityMessenger
import com.jjc.android.util.paramEx
import com.jjc.android.util.showImage
import com.jjc.android.view.layout.*
import com.jjc.android.view.onClick
import com.lzy.okgo.OkGo

class AddSamplingActivity : BaseActivity(){

    companion object{
        fun start(context: Activity,classId:String,action:(Boolean)->Unit){
            val intent = Intent(context,AddSamplingActivity::class.java)
            intent.putExtra("classId",classId)
            ActivityMessenger.startActivityForResult(context,intent){
                (it?.getBooleanExtra(Constants.BUNDLE_INFO,false)==true).let(action)
            }
        }
    }
    private val okBtn: TextView by lazy { findViewById(R.id.okBtn) }
    private val homeBtn: ImageView by lazy { findViewById<ImageView>(R.id.homeBtn) }
    private val samplingImageLayout: LinearLayout by lazy { findViewById(R.id.samplingImageLayout) }
    private val contentText:TextView by lazy { findViewById(R.id.contentText) }

    override val layoutId: Int = R.layout.activity_add_sampling
    private var classId:String = ""
    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.sampling_txt))
        AndroidBug5497Workaround.assistActivity(this)
        homeBtn.visibility = View.VISIBLE
        homeBtn.onClick {
            MessageImageDialog.Builder(this)
                .imageResId(R.mipmap.alert_jiancejindu_pic)
                .content(getString(R.string.dete_not_save_txt))
                .okClick {
                    MainActivity.start(this)
                }
                .build()
                .show()
        }
        okBtn.onClick {
            setResult(Activity.RESULT_OK, Intent().apply {
                putExtra(Constants.BUNDLE_INFO,true)
            })
            finish()
        }
        fetchPageInfo()
    }

    override fun fetchPageInfo() {
        OkGo.post<LzyResponse<PageInfo<SamplingPageDto>>>(Constants.getFormList)
            .paramEx(mutableMapOf("page" to 1,"pagesize" to 1,"needpage" to 1,"formheaduuid" to "68b6063091a54242a0d3b7794ef14529","filters" to listOf<KeyValuePair>(
                KeyValuePair("ktrpe1fzjzsdoek8",classId),
                KeyValuePair("vydvtklk0xjgsinm",MultiLanguageUtil.getInstance().getValue("746f5cd0-dee7-4a10-8f91-75b27ac123b4","69bd0b96-e704-4522-8543-4c140691793c")),
                KeyValuePair("xkw6hzbgrureov83",MultiLanguageUtil.getInstance().getValue("a6d76715-3f36-44b9-bb1b-3328eb146b07","0e6a8294-62bf-497c-8aee-ef984f797896"))
            )),true)
            .tag(this)
            .execute(object : SimpleHttpCallback<LzyResponse<PageInfo<SamplingPageDto>>>(){
                override fun onCallSuccess(response: LzyResponse<PageInfo<SamplingPageDto>>) {
                    response.data?.first?.let {
                        val images = it.jsonMember9rjnk9bcnstxhdli
                        if(images.isNullOrEmpty()){
                            samplingImageLayout.visibility = View.GONE
                        }else{
                            samplingImageLayout.visibility = View.VISIBLE
                            samplingImageLayout.removeAllViews()
                            images.forEach { image->
                                samplingImageLayout.ImageView {
                                    layout_width = match_parent
                                    layout_height = wrap_content
                                    scaleType = ImageView.ScaleType.FIT_XY
                                    adjustViewBounds = true
                                    margin_bottom = 6
                                    showImage(image.url)
                                }
                            }
                        }
                        contentText.text = it.ovoqe3wroqadzqso.parseEmpty()

                    }
                }
            })
    }

    override fun setupArguments(args: Bundle?) {
        classId = args?.getString("classId")?:""
    }

}